/*
 * ChatRemindManager.java
 *
 * Created on 2007年5月17日, 下午2:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.manager;

import com.ecomm.chat.pojo.ChatMessage;
import com.ecomm.chat.pojo.ChatRemind;
import com.ecomm.platcore.service.BaseEntityManager;
import com.ecomm.util.HibernateUtil;
import com.ecomm.util.SpringUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;


/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月17日, 下午2:40
 */
public class ChatRemindManager  extends BaseEntityManager<ChatRemind> {
  
  /** Creates a new instance of ChatRemindManager */
  public ChatRemindManager() {
  }
  private static ChatRemindManager chatRemindManager = null;
  
  public static ChatRemindManager getInstance(){
    if(chatRemindManager == null){
      chatRemindManager = (ChatRemindManager)SpringUtil.getBean("chatRemindManager");
    }
    return chatRemindManager;
  }
  /**
   *查询有多少个新记录
   */
  public String getCount(String userid){
    
    if(userid== null || userid.equals("")){
      return "0";
    }
    String hql = "select count(r.id) from ChatRemind r where  r.state ='0' and r.acceptuserid = '"+userid+"'";
    
    return this.find(hql).toString();
  }
  /**
   *判断是否需要图层提醒用户呢？
   */
  public String getRemindCount(String userid){
    
    if(userid== null || userid.equals("")){
      return "0";
    }
    String hql = "select count(r.id) from ChatRemind r,ChatUserInfo u  where u.imgtype ='1' and  r.state ='0' and r.acceptuserid = u.userid  and r.acceptuserid = '"+userid+"'";
    
    return this.find(hql).toString();
  }
  /**
   *判断是否需要声音提醒用户呢？
   */
  public String getRemindSound(String userid){
    if(userid== null || userid.equals("")){
      return "0";
    }
    String hql = "select count(r.id) from ChatRemind r,ChatUserInfo u  where u.soundtype ='1' and  r.state ='0' and r.acceptuserid = u.userid  and r.acceptuserid = '"+userid+"'";
    return this.find(hql).toString();
  }
  /**
   *判断是否需要弹出窗口提醒用户呢？
   *
   */
  public String getRemindWindowOpen(String userid){
    if(userid== null || userid.equals("")){
      return "0";
    }
    String hql = "select count(r.id) from ChatRemind r,ChatUserInfo u  where u.fliptype ='1' and  r.state ='0' and r.acceptuserid = u.userid  and r.acceptuserid = '"+userid+"'";
    return this.find(hql).toString();
  }
  /**
   *非工作流
   *只是查看消息
   * c.id,c.message , c.url ,c.senduserid,c.sendtime,c.type
   * count(c.senduserid),c.senduserid ,c.parentid,c.type
   */
  public void updateRemindState(String senduserid,String state,String parentid,String acceptuserid,String type,String bigtype){
    List list  =  this.getHibernateTemplate().find("from ChatMessage m where type ='"+type+"' and m.bigtype ='"+bigtype+"'  and m.parentid = '"+parentid+"' and m.senduserid ='"+senduserid+"'");
    if(list != null && list.size() >0){
      for (int i = 0; i < list.size(); i++) {
        ChatMessage cm = (ChatMessage)list.get(i);
        Set set = (Set)cm.getChatRemind();
        java.util.Iterator it_object = set.iterator();
        while(it_object.hasNext()){
          ChatRemind cr = (ChatRemind)it_object.next();
          if(cr.getAcceptuserid() != null && cr.getAcceptuserid().equals(acceptuserid)){
            cr.setState(state);
            this.getHibernateTemplate().update(cr);
          }
        }
      }
    }
  }
  /**
   *适用工作流
   *c.id,c.message , c.url ,c.senduserid,c.sendtime,c.type
   */
  public void updateRemindStateWorkflow(String id,String state){
    
    ChatRemind cr =(ChatRemind)this.find("from ChatRemind where c.chatMessage.id= '"+id+"'");
    cr.setState(state);
    this.getHibernateTemplate().update(cr);
  }
  public void delete(String id){
    if(id != null && !id.equals("")){
      this.removeById(ChatMessage.class,id);
    }
  }
  /**
   *先查出本消息在消息提醒表中有多少条记录，假如只有1条，则把CHATMESSAGE AND CHATREMID的记录全部删除
   *反之就删除CHATREMIND的本人的一条消息
   *
   * count(c.senduserid),c.senduserid ,c.parentid,c.type
   *c.id,c.message , c.url ,c.senduserid,c.sendtime,c.type
   */
  public void deleteRemindWorkFlow(String id,String acceptuserid,boolean bool){
    if(id != null && !id.equals("")){
      String hql = "from ChatRemind where chatMessage.id ='"+id+"'";
      List list = this.getHibernateTemplate().find(hql);
      if(list != null && list.size() >0 ){
        for (int i = 0; i < list.size(); i++) {
          ChatRemind chatr = (ChatRemind)list.get(i);
          if(chatr!=null &&  bool){ // 发送人 如果是 接收人
            getHibernateTemplate().delete(chatr);
            continue;
          } else  if(chatr!=null && chatr.getAcceptuserid() != null && chatr.getAcceptuserid().equals(acceptuserid)){
            getHibernateTemplate().delete(chatr);
            break;
          }
        }
      }
      if(bool){ // 发送人 如果是 接收人  就删除总消息
        ChatMessage cm = (ChatMessage)this.find("from ChatMessage where id = '"+id+"'");
        if(cm != null){
          getHibernateTemplate().delete(cm);
        }
      }
    }
  }
  public  Object find(String hql){
    List list =  this.getHibernateTemplate().find(hql);
    if(list != null && list.size() >0 ){
      return list.get(0);
    }else{
      return null;
    }
  }
}
